import axios from "axios";
import { AxiosHttpAdapter, Kinnara } from '@fatewa/kinnara'
import api from './api'
export const BASE_URL = import.meta.env.VITE_BASE_URL

export const instance = axios.create({
  baseURL: BASE_URL
})

instance.interceptors.request.use(async (r) => {
  const gateway = r.headers?.gateway ?? BASE_URL
  if (gateway) {
    r.baseURL = gateway
  }
  return r
})

const kinnara = new Kinnara()
// 设置 http 适配器
kinnara.setHttpAdapter(new AxiosHttpAdapter(instance))

/**
 * 导出对外的请求对象, 关于 kinnara 的使用请参考{@link https://github.com/4everlynn/kinnara}
 * @type {any}
 * @private
 */
export const _ = kinnara.proxy(api)
export {
  api
}
